package com.breeze.blockchain.common.gen;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import com.breeze.components.mybatis.base.BaseEntity;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.template.impl.EnjoyTemplate;
import com.zaxxer.hikari.HikariDataSource;
import java.io.File;

/**
 * 代码生成
 *
 * @author breeze
 */
public class GenTests {

  public static void main(String[] args) {
    // 配置数据源
    HikariDataSource dataSource = new HikariDataSource();
    dataSource.setJdbcUrl(
        "jdbc:mysql://localhost:3306/breeze_db?useInformationSchema=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
    dataSource.setUsername("root");
    dataSource.setPassword("Lj@13152022518");

    // 创建配置内容
    GlobalConfig globalConfig =
        createGlobalConfigUseStyle("sys_file_detail", "sys_file_part_detail");

    // 通过 datasource 和 globalConfig 创建代码生成器
    Generator generator = new Generator(dataSource, globalConfig);

    // 生成代码
    generator.generate();
  }

  private static GlobalConfig createGlobalConfigUseStyle(String... tables) {
    if (ArrayUtil.isEmpty(tables)) {
      return null;
    }

    // 创建配置内容
    GlobalConfig globalConfig = new GlobalConfig();

    String base = new File(System.getProperty("user.dir")) + "/";
    String baseSrcResources = base + "breeze-blockchain/src/main/resources/";
    String baseSrcMain = base + "breeze-blockchain/src/main/java/";
    globalConfig
        .getTemplateConfig()
        .setTemplate(new EnjoyTemplate())
        .setEntity(baseSrcResources + "template/entityOrBase.tpl")
        .setTableDef(baseSrcResources + "template/tableDef.tpl")
        .setMapper(baseSrcResources + "template/mapper.tpl")
        .setMapperXml(baseSrcResources + "template/mapperXml.tpl")
        .setService(baseSrcResources + "template/service.tpl")
        .setServiceImpl(baseSrcResources + "template/serviceImpl.tpl")
        .setController(baseSrcResources + "template/controller.tpl");

    // 设置根包
    globalConfig
        .getPackageConfig()
        .setSourceDir(baseSrcMain)
        .setMapperXmlPath(baseSrcResources + "/mapper")
        .setBasePackage("com.breeze.blockchain")
        .setEntityPackage("com.breeze.blockchain.mybatis.entity")
        .setTableDefPackage("com.breeze.blockchain.mybatis.entity.table")
        .setMapperPackage("com.breeze.blockchain.mybatis.mapper")
        .setControllerPackage("com.breeze.blockchain.controller.admin");

    // 设置表前缀和只生成哪些表
    globalConfig
        .getStrategyConfig()
        .setTablePrefix("sys_")
        .setGenerateTable(tables)
        .setIgnoreColumns(
            "id",
            "createTime",
            "create_time",
            "updateTime",
            "update_time",
            "createBy",
            "create_by",
            "updateBy",
            "update_by",
            "version",
            "deleteFlag",
            "delete_flag");

    // 设置生成 entity 并启用 Lombok
    globalConfig
        .enableEntity()
        .setJdkVersion(17)
        .setSwaggerVersion(EntityConfig.SwaggerVersion.DOC)
        .setWithLombok(true)
        .setClassSuffix("Do")
        .setWithLombok(true)
        .setSuperClass(BaseEntity.class)
        .setOverwriteEnable(false);
    // globalConfig
    //     .enableTableDef()
    //     .setPropertiesNameStyle(TableDefConfig.NameStyle.UPPER_CAMEL_CASE)
    //     .setOverwriteEnable(false);

    // 设置生成 mapper
    globalConfig.enableMapper().setMapperAnnotation(true).setOverwriteEnable(false);
    globalConfig.enableMapperXml().setOverwriteEnable(false);

    // service
    globalConfig.enableService().setOverwriteEnable(false);
    globalConfig.enableServiceImpl().setOverwriteEnable(false);

    // controller
    globalConfig
        .enableController()
        .setRequestMappingPrefix("/api/admin")
        .setRestStyle(true)
        .setOverwriteEnable(false);

    // doc
    globalConfig.getJavadocConfig().setAuthor("breeze").setSince(DateUtil.now());
    return globalConfig;
  }
}
